package LC;

/**
 * https://leetcode.com/problems/candy/description/
 * There are N children standing in a line. Each child is assigned a rating value.
 * You are giving candies to these children subjected to the following requirements:
 * Each child must have at least one candy.
 * Children with a higher rating get more candies than their neighbors.
 * What is the minimum candies you must give?
 */
public class LC_135_Candy_DP_Greedy {
    public static void main(String[] args) {
        System.out.println(Solution.candy(new int[]{1, 4, 2, 5, 21, 46, 42, 1, 2, 34}));
    }

    static class Solution {
        static int candy(int[] ratings) {
            int length = ratings.length;
            if (length <= 0) return 0;
            int[] nums = new int[length];
            nums[0] = 1;

            for (int i = 1; i < nums.length; i++) {
                nums[i] = 1;
                if (ratings[i] > ratings[i - 1])
                    nums[i] = nums[i - 1] + 1;
            }

            for (int i = nums.length - 2; i >= 0; i--)
                if (ratings[i] > ratings[i + 1])
                    nums[i] = Math.max(nums[i], nums[i + 1] + 1);

            int count = 0;

            for (int i = 0; i < length; i++) count += nums[i];
            return count;
        }
    }
}
